package com.dearbinge.utils.httpClient;


import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.protocol.HttpClientContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.util.List;

public final class ApiResponse {

	private static Logger LOG = LoggerFactory.getLogger(ApiResponse.class);
	private HttpResponse httpResponse;
	private HttpClientContext context;
	private String body;
	private final byte[] content;

    /**
     * @param content
     */
    ApiResponse(HttpResponse httpResponse, HttpClientContext context, byte[] content) {
    	this.httpResponse = httpResponse;
        this.content = content;
        this.context = context;
        try {
			this.body = new String(content,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			LOG.error(e.getMessage(), e);
		}
    }

    public boolean isOk() {
    	return getStatusCode() == 200 || getStatusCode() == 201;
    }

    public int getStatusCode() {
    	return httpResponse.getStatusLine().getStatusCode();
    }

    public HttpClientContext getContext() {
    	return this.context;
    }

    public HttpResponse getHttpResponse() {
		return httpResponse;
	}

    public Header[] getAllHeaders() {
    	if (this.httpResponse == null) return null;
    	return this.httpResponse.getAllHeaders();
    }

    public Header[] getHeaders(String param) {
    	if (this.httpResponse == null) return null;
    	return this.getHttpResponse().getHeaders(param);
    }

	public byte[] getContent() {
    	return this.content;
    }

    /**
     * @param type
     * @param <T>
     * @return
     */
//    public <T> List<T> getResultList(Class<T> type) {
//        return ApiHttpCodec.convertToList(this.body, type);
//    }
//
//    public <T> T getResult(Class<T> type) {
//        return ApiHttpCodec.convertAsType(this.body, type);
//    }

    public String toString() {
    	return this.body;
    }


    public static void main(String[] args) {
    	ApiResponse api = new ApiRequest("http://blog.sina.com.cn/s/blog_189b318270102y7wv.html", ApiRequest.Verb.GET).charset("UTF-8")
    			.params("school_code", "1111")
    			.params("school_code", "dddddddddddddddd").send();
        try {
			String result = new String(api.getContent(), "UTF-8");
			System.out.println("result============="+result);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
